package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ruoyi.common.utils.JsonResult;
import com.ruoyi.system.domain.GuestbookAsk;
import com.ruoyi.system.domain.SessionLoginUser;
import com.ruoyi.system.service.IGuestbookAskService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GuestbookReply;
import com.ruoyi.system.service.IGuestbookReplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/system/reply")
public class GuestbookReplyController extends BaseController
{
    @Autowired
    private IGuestbookReplyService guestbookReplyService;

    @Autowired
    private IGuestbookAskService guestbookAskService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:reply:list')")
    @GetMapping("/list")
    public TableDataInfo list(GuestbookReply guestbookReply)
    {
        startPage();
        List<GuestbookReply> list = guestbookReplyService.selectGuestbookReplyList(guestbookReply);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:reply:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GuestbookReply guestbookReply)
    {
        List<GuestbookReply> list = guestbookReplyService.selectGuestbookReplyList(guestbookReply);
        ExcelUtil<GuestbookReply> util = new ExcelUtil<GuestbookReply>(GuestbookReply.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:reply:query')")
    @GetMapping(value = "/{replyid}")
    public AjaxResult getInfo(@PathVariable("replyid") String replyid)
    {
        return success(guestbookReplyService.selectGuestbookReplyByReplyid(replyid));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:reply:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GuestbookReply guestbookReply)
    {
        return toAjax(guestbookReplyService.insertGuestbookReply(guestbookReply));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:reply:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GuestbookReply guestbookReply)
    {
        return toAjax(guestbookReplyService.updateGuestbookReply(guestbookReply));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:reply:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{replyids}")
    public AjaxResult remove(@PathVariable String[] replyids)
    {
        return toAjax(guestbookReplyService.deleteGuestbookReplyByReplyids(replyids));
    }

    @RequestMapping("add")
    @ResponseBody
    public JsonResult<?> add(@RequestParam("askid") String ppAskId,
                             @RequestParam("content") String ppContent, HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            SessionLoginUser mmLogin = (SessionLoginUser)session.getAttribute("sessionlogin");
            String mmUserId = mmLogin.getLoginUserId();
            if (mmUserId==null){
                return JsonResult.getErrorResult("请先登录！");
            }

            GuestbookAsk mmGuestbookAsk =guestbookAskService.selectGuestbookAskByAskid(ppAskId);
            if (mmGuestbookAsk == null) {
                return JsonResult.getErrorResult("不存在该留言！");
            }
            GuestbookReply mmGuestbookReply = new GuestbookReply();

            String mmReplyId = UUID.randomUUID().toString();
            mmGuestbookReply.setReplyid(mmReplyId);
            mmGuestbookReply.setAskid(ppAskId);
            mmGuestbookReply.setHuifurenid(mmUserId);
            mmGuestbookReply.setHuifuren(mmLogin.getLoginName());
            mmGuestbookReply.setContent(ppContent);
            mmGuestbookReply.setHuifushijian(new Date());

            if (guestbookReplyService.insertGuestbookReply(mmGuestbookReply)==1) {
                return JsonResult.getSuccessResult("回复成功！");
            }else {
                return JsonResult.getErrorResult("回复失败！");
            }

        } catch (Exception e) {
            return JsonResult.getErrorResult(e.getMessage());
        }
    }
}
